<?php
/*
 * @Description: 
 * @Author: Leequik
 * @Date: 2025-07-23 21:49:10
 * @LastEditTime: 2025-08-12 23:35:32
 * @LastEditors: Leequik
 */

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use App\Events\TestEvent;
use App\Services\SendMessageService;
use App\Jobs\SendMsgJob;
use Illuminate\Support\Facades\Redis;


class TestController extends Controller{


    public function eventTest(){
        //调用事件
        $argType = 'XDJ';
        $expire = '1';
        $msg = '发欺诈超时警告！';
        event(new TestEvent($argType,$expire,$msg));
        
    }


    public function match_test($type){
        $argType = 'XDJ';
        $expire = '1';
        $msg = '发欺诈超时警告！';
        
        $res = match($type){
            'a' => $this->lala(),
            'b' => event(new TestEvent($argType,$expire,$msg)),
            'c' => 'C',
            default => 'default value',
        };
    }


    public function lala(){
        echo '道爷我成了！';
    }


    public function sed(){
        //$this->match_test('a');
        //$this->match_test('b');

        $this->sendMsgJob('c');
       //$this->redisTest();
    }


    public function sendMsgJob($type){
        //调用服务
        if($type && in_array($type, ['a', 'b', 'c'])) { 

         SendMsgJob::dispatch($type)->delay(now()->addMinutes(2));

         return response()->json(['message' => 'Job dispatched for type: ' . $type]);
        
        }
    }
    // Redis connection test
    public function redisTest() {
       
            // Test Redis connection
            Redis::set('test_key', 'Hello, Redis!');
            $value = Redis::get('test_key');
            echo $value; // Output: Hello, Redis!

            return response()->json(['message' => 'Redis connection successful!', 'value' => $value]);
       
    }


    public function fun() {
        
        $this->fun1();
        $this->fun5();
    }

    public function fun1() {
        
        if($this->fun2()){

            return $this->fun6();
        }

        if($this->fun3()){

            return $this->fun6();
        }

        if($this->fun4()){

            return $this->fun6();
        }

    
    }

    public function fun2() {

        echo "fun2";
        return true;
    }

    public function fun3() {
        echo "fun3";
        return false;
    }

    public function fun4() {
        echo "fun4";
        return true;
    }

    public function fun5() {
        echo "fun5";
    }

    public function fun6() {
        echo "fun6";
    }

    public function fun7() {
        echo "fun6";
    }


    //筛选demo
    public function filterDemo(){ 
        $demoArr['data'] = [
                0 => [
                    'chn' => '张三',
                    'eng' => 'zs',
                    'rate' => 30,
                    'position' => '1;2',
 
                ],
                1 => [
                    'chn' => '李四',
                    'eng' => 'ls',
                    'rate' => 50,
                    'position' => '1',
                ],
                2 => [
                    'chn' => '王五',
                    'eng' => 'ww',
                    'rate' => 60,
                    'position' => '1;2',
                ],
                3 => [
                    'chn' => '赵六',
                    'eng' => 'zl',
                    'rate' => 70,
                    'position' => '2',
                ],
                4 => [
                    'chn' => '孙七',
                    'eng' => 'sq',
                    'rate' => 80,
                    'position' => '1;2',
                ],
                5 => [
                    'chn' => '王五',
                    'eng' => 'ww',
                    'rate' => 90,
                    'position' => '2',
                ],
                6 => [
                    'chn' => '吴九',
                    'eng' => 'wj',
                    'rate' => 100,
                    'position' => '1',
                ],
            ];
        // $demoArr['data'] = [
            
        //         'chn' => '张三',
        //         'eng' => 'zs',
        //         'rate' => 30,
        //         'position' => '1;2',

        //     [
        //         'chn' => '李四',
        //         'eng' => 'ls',
        //         'rate' => 50,
        //         'position' => '1',
        //     ],
        //     [
        //         'chn' => '王五',
        //         'eng' => 'ww',
        //         'rate' => 60,
        //         'position' => '1;2',
        //     ],
            
        //         'chn' => '赵六',
        //         'eng' => 'zl',
        //         'rate' => 70,
        //         'position' => '2',
            
        //     [
        //         'chn' => '孙七',
        //         'eng' => 'sq',
        //         'rate' => 80,
        //         'position' => '1;2',
        //     ],
        //     [
        //         'chn' => '王五',
        //         'eng' => 'ww',
        //         'rate' => 90,
        //         'position' => '2',
        //     ],
        //     [
        //         'chn' => '吴九',
        //         'eng' => 'wj',
        //         'rate' => 100,
        //         'position' => '1',
        //     ],
        // ];

            
            // $filterArr = array_filter($demoArr, function ($item) {
            //     return $item['rate'] >= 40;
            // });
            $res['data'] = [];
            $chnArr = [];
            $engArr = [];
            $rateArr = [];

            foreach ($demoArr['data'] as $k => $mebmer){
                
                
                $positionArr = explode(';', $mebmer['position']);
                //股东
                if(in_array(2,$positionArr)){

                    if(in_array($mebmer['chn'],$chnArr) || in_array($mebmer['eng'],$engArr)){
                        //$mebmer  = implode('|', $array_filter([$mebmer['chn'],$mebmer['eng']]));
                        $mebmerkey = implode('|', array_filter([$mebmer['chn'], $mebmer['eng']], function ($value) {
                            return !empty($value);
                        }));

                        $rateArr[$mebmerkey] = $mebmer['rate'];
                        unset($demoArr['data'][$k]);
                        continue;
                    }

                    
                }
                $chnArr[] = $mebmer['chn'];
                $engArr[] = $mebmer['eng'];
                //$res['data'][$k] = $mebmer;
                $res['data'] = $demoArr['data'];
            }
            //dd($res);
            //dd($rateArr);

            //将最后一级的股权赋给独一层的股东
            foreach ($res['data'] as $key => $mebmer) { 
                $mebmerkey = implode('|', array_filter([$mebmer['chn'], $mebmer['eng']], function ($value) {
                    return !empty($value);
                }));
                if (isset($rateArr[$mebmerkey])) {
                    $res['data'][$key]['rate'] = $rateArr[$mebmerkey];
                }
            }
            dd($res);

    }

    public function filterTest(){
        $membersArr = [
            '张三|zs' => [
                'chn' => '张三',
                'eng' => 'zs',
                'rate' => 30,
                'position' => '1;2',

            ],
            1 => [
                'chn' => '李四',
                'eng' => '',
                'rate' => 50,
                'position' => '1',
            ],
            'ww' => [
                'chn' => '',
                'eng' => 'ww',
                'rate' => 60,
                'position' => '1;2',
            ],
            '张三' => [
                'chn' => '张三',
                'eng' => '',
                'rate' => 70,
                'position' => '2',
            ],
            'zs' => [
                'chn' => '',
                'eng' => 'zs',
                'rate' => 70,
                'position' => '2',
            ],
            4 => [
                'chn' => '孙七',
                'eng' => 'sq',
                'rate' => 80,
                'position' => '1;2',
            ],
            5 => [
                'chn' => '王五',
                'eng' => 'ww',
                'rate' => 90,
                'position' => '2',
            ],
            6 => [
                'chn' => '吴九',
                'eng' => 'wj',
                'rate' => 100,
                'position' => '1',
            ],
        ];
    //laravel10 过滤上述数组 $membersArr， 循环过滤掉后面一唯数组中 chn键 或 eng键  的值在前面出现过的数组，
    //并将该数组中rate项赋值给首次出现的相同的chn键 或 eng键的值的数组中的rate项
    }
//只过滤不替换chn eng
function filterMembers(array $membersArr): array
{

    $result = [];
    $valueMap = []; // 存储值到首次出现键的映射

    foreach ($membersArr as $key => $item) {
        $isDuplicate = false;
        $firstOccurrenceKey = null;

        // 检查 chn 值是否重复
        if (!empty($item['chn'])) {
            $chnValue = $item['chn'];
            if (isset($valueMap[$chnValue])) {
                $isDuplicate = true;
                $firstOccurrenceKey = $valueMap[$chnValue];
            }
        }

        // 如果 chn 未重复，检查 eng 值
        if (!$isDuplicate && !empty($item['eng'])) {
            $engValue = $item['eng'];
            if (isset($valueMap[$engValue])) {
                $isDuplicate = true;
                $firstOccurrenceKey = $valueMap[$engValue];
            }
        }

        if ($isDuplicate) {
            // 检查条件：position 包含 '2' 且 rate > 50
            $positions = explode(';', $item['position']);
            if (in_array('2', $positions) && $item['rate'] > 50) {
                // 替换首次出现项的 rate
                $result[$firstOccurrenceKey]['rate'] = $item['rate'];
            }
            // 无论是否替换，都移除当前重复项
        } else {
            // 添加新元素到结果
            $result[$key] = $item;
            
            // 在映射中记录新值
            if (!empty($item['chn'])) {
                $valueMap[$item['chn']] = $key;
            }
            if (!empty($item['eng'])) {
                $valueMap[$item['eng']] = $key;
            }
        }
    }

    return $result;
}

//只过滤不替换chn eng,且 level==2
function filterMembersY(array $membersArr): array
{

    $result = [];
    $valueMap = []; // 存储值到首次出现键的映射

    foreach ($membersArr as $key => &$item) {
        $isDuplicate = false;
        $firstOccurrenceKey = null;

        // 检查 chn 值是否重复
        if (!empty($item['chn'])) {
            $chnValue = $item['chn'];
            if (isset($valueMap[$chnValue])) {
                $isDuplicate = true;
                $firstOccurrenceKey = $valueMap[$chnValue];
            }
        }

        // 如果 chn 未重复，检查 eng 值
        if (!$isDuplicate && !empty($item['eng'])) {
            $engValue = $item['eng'];
            if (isset($valueMap[$engValue])) {
                $isDuplicate = true;
                $firstOccurrenceKey = $valueMap[$engValue];
            }
        }

        if ($isDuplicate) {
            // 检查条件：position 包含 '2' 且 rate > 50
            $positions = explode(';', $item['position']);
            if ($item['level'] == 2 && in_array('2', $positions) && $item['rate'] > 50) {
                // 替换首次出现项的 rate
                $result[$firstOccurrenceKey]['rate'] = $item['rate'];
            }
            // 无论是否替换，都移除当前重复项
        } else {
            // 添加新元素到结果
            $result[$key] = $item;
            
            // 在映射中记录新值
            if (!empty($item['chn'])) {
                $valueMap[$item['chn']] = $key;
            }
            if (!empty($item['eng'])) {
                $valueMap[$item['eng']] = $key;
            }
        }
    }

    return $result;
}





//过滤并替换存在的chn和eng
function filterMembers2(array $membersArr): array
{
    $result = [];
    $valueMap = []; // 存储值到首次出现键的映射

    foreach ($membersArr as $key => $item) {
        $isDuplicate = false;
        $firstOccurrenceKey = null;

        // 检查 chn 值是否重复
        if (!empty($item['chn'])) {
            $chnValue = $item['chn'];
            if (isset($valueMap[$chnValue])) {
                $isDuplicate = true;
                $firstOccurrenceKey = $valueMap[$chnValue];
            }
        }

        // 如果 chn 未重复，检查 eng 值
        if (!$isDuplicate && !empty($item['eng'])) {
            $engValue = $item['eng'];
            if (isset($valueMap[$engValue])) {
                $isDuplicate = true;
                $firstOccurrenceKey = $valueMap[$engValue];
            }
        }

        if ($isDuplicate) {
            // 获取首次出现项的引用
            $firstItem = &$result[$firstOccurrenceKey];
            
            // 检查并更新空字段
            if (empty($firstItem['chn']) && !empty($item['chn'])) {
                $firstItem['chn'] = $item['chn'];
            }
            if (empty($firstItem['eng']) && !empty($item['eng'])) {
                $firstItem['eng'] = $item['eng'];
            }
            
            // 检查条件：position 包含 '2' 且 rate > 50
            $positions = explode(';', $item['position']);
            if (in_array('2', $positions) && $item['rate'] > 50) {
                // 替换首次出现项的 rate
                $firstItem['rate'] = $item['rate'];
            }
            // 无论是否替换，都移除当前重复项
        } else {
            // 添加新元素到结果
            $result[$key] = $item;
            
            // 在映射中记录新值
            if (!empty($item['chn'])) {
                $valueMap[$item['chn']] = $key;
            }
            if (!empty($item['eng'])) {
                $valueMap[$item['eng']] = $key;
            }
        }
    }

    return $result;
}

//过滤并替换存在的chn和eng,且lavel>1的rate
function filterMembers3(array $membersArr): array
{
    $result = [];
    $valueMap = []; // 存储值到首次出现键的映射

    foreach ($membersArr as $key => $item) {
        $isDuplicate = false;
        $firstOccurrenceKey = null;

        // 检查 chn 值是否重复
        if (!empty($item['chn'])) {
            $chnValue = $item['chn'];
            if (isset($valueMap[$chnValue])) {
                $isDuplicate = true;
                $firstOccurrenceKey = $valueMap[$chnValue];
            }
        }

        // 如果 chn 未重复，检查 eng 值
        if (!$isDuplicate && !empty($item['eng'])) {
            $engValue = $item['eng'];
            if (isset($valueMap[$engValue])) {
                $isDuplicate = true;
                $firstOccurrenceKey = $valueMap[$engValue];
            }
        }

        if ($isDuplicate) {
            // 获取首次出现项的引用
            $firstItem = &$result[$firstOccurrenceKey];
            
            // 更新空字段
            if (empty($firstItem['chn']) && !empty($item['chn'])) {
                $firstItem['chn'] = $item['chn'];
            }
            if (empty($firstItem['eng']) && !empty($item['eng'])) {
                $firstItem['eng'] = $item['eng'];
            }
            
            // 检查条件：level=2, position包含'2'，且rate>50
            $positions = explode(';', $item['position']);
            if ($item['level'] == 2 && in_array('2', $positions) && $item['rate'] > 50) {
                // 替换首次出现项的 rate
                $firstItem['rate'] = $item['rate'];
            }
            // 无论是否替换，都移除当前重复项
        } else {
            // 添加新元素到结果
            $result[$key] = $item;
            
            // 在映射中记录新值
            if (!empty($item['chn'])) {
                $valueMap[$item['chn']] = $key;
            }
            if (!empty($item['eng'])) {
                $valueMap[$item['eng']] = $key;
            }
        }
    }

    return $result;
}


      
public function test(){
    $membersArr = [
        '张三|zs' => [
            'chn' => '张三',
            'eng' => 'zs',
            'rate' => 30,
            'position' => '1;2',
            'level' => 1,

        ],
        1 => [
            'chn' => '李四',
            'eng' => '',
            'rate' => 50,
            'position' => '1',
            'level' => 2,
        ],
        'ww' => [
            'chn' => '',
            'eng' => 'ww',
            'rate' => 60,
            'position' => '1;2',
            'level' => 2,
        ],
        '张三' => [
            'chn' => '张三',
            'eng' => '',
            'rate' => 70,
            'position' => '2',
            'level' => 2,
        ],
        'zs' => [
            'chn' => '',
            'eng' => 'zs',
            'rate' => 60,
            'position' => '2',
            'level' => 2,
        ],
        4 => [
            'chn' => '孙七',
            'eng' => 'sq',
            'rate' => 80,
            'position' => '1;2',
            'level' => 1,
        ],
        5 => [
            'chn' => '王五',
            'eng' => 'ww',
            'rate' => 90,
            'position' => '2',
            'level' => 1,
        ],
        6 => [
            'chn' => '吴九',
            'eng' => 'wj',
            'rate' => 100,
            'position' => '1',
            'level' => 2,
        ],
    ];

    //$result = $this->filterMembers2($membersArr);

    //项目需求方法
    $result = $this->filterMembersY($membersArr);


    dd($result);

}


}